/**
 * 带头,单向,不循环链表
 */
public class MySingleList implements IList {
    //通过内部类,创建一个节点
    static class ListNode {
        public int val;//数值
        public ListNode next;//指向下一节点
        public ListNode(int val) {
            this.val = val;
        }
    }
    //属性,链表头节点
    public ListNode head;
    //串联链表
    public void createList() {
        //创建链表
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        //连接链表
        this.head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;
    }

    /**
     * 打印链表
     */
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    /**
     * 查找key
     */
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {//遍历链表是否存在key
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    /**
     * 求节点个数
     */
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    /**
     * 头插法
     */
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {//原本为空
            head = node;
        }else {
            ListNode cur = head;
            //找尾
            while(cur.next != null) {
                cur = cur.next;
            }
            //插入
            cur.next = node;
        }
    }
    /**
     * 任意位置插入
     */
    public void addIndex(int index,int data) throws IndexException{
        ListNode node = new ListNode(data);
        //判断index是否合理
        if(index < 0 || index > size()) {
            throw new IndexException("index不合法,index为: " + index);
        }
        //头
        if(index == 0) {
            addFirst(data);
            return;
        }
        //尾
        if(index == size() - 1) {
            addLast(data);
            return;
        }
        //中间
        ListNode cur = searchPrevIndex(index);//找到插入位置前一个节点位置
        //先后再前
        node.next = cur.next;
        cur.next = node;
    }
    /**
     * 查找index前一个节点位置
     */
    private ListNode searchPrevIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while(count != index - 1) {//下标前一位
            cur = cur.next;
            count++;
        }
        return cur;
    }
    /**
     * 删除val为key的节点
     */
    public void remove(int key) {
        //为空
        if(head == null) {
            return;
        }
        //首元素为key
        if(head.val == key) {
            head = head.next;
        }
        //不在head
        ListNode cur = searchPrevKey(key);
        if(cur == null) {
            return;
        }
        //删除
            cur.next =cur.next.next;
    }
    /**
     * 寻找key前驱节点
     */
    private ListNode searchPrevKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }else {
                cur = cur.next;
            }
        }
        return null;
    }

    /**
     * 删除链表中所有val为key的节点
     * @param key
     */
    public void  removeAllKey(int key) {
        //为空
        if(head == null) {
            return;
        }
        ListNode cur = head.next;
        ListNode prev  = head;
        //删除除head以外所有val为key的节点
        while(cur != null) {
            //cur.val == key,prev.next越过cur节点
            if(cur.val == key) {
                prev.next = cur.next;
                cur = prev.next;
            }else {//不等于,prev++,cur++
                cur = cur.next;
                prev = prev.next;
            }
        }
        //其他节点均已经删除,处理头节点
        if(head.val == key) {
            head = head.next;
        }
    }
    /**
     * 清空所有节点
     */
    public void clear() {
        head = null;
    }
    /**
     * 反转链表
     */
    public ListNode reverseList() {

        //为空
        if(head == null) {
            return null;
        }
        //只有一个节点
        if(head.next == null) {
            return head;
        }
        //1.头节点next为null
        //2.头插法
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {//遍历
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}
